{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f4ef4f4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "print(123)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ebf1c41",
   "metadata": {},
   "source": [
    "print(123)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fee79b5",
   "metadata": {},
   "source": [
    "### 브로드캐스트 메커니즘 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0220b6c6",
   "metadata": {},
   "source": [
    "#### 1차원 배열 브로드캐스트"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c1717a63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[0,1,2,3] * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3eba6ed8",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [3, 4, 5],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# sort 정렬\n",
    "# [0,1,2,3]*3\n",
    "arr1 = np.sort(np.array([0,1,2,3]*3)).reshape(4,3) #shape(4,3)\n",
    "display(arr1)\n",
    "arr2 = np.array([1,2,3]) # shape(3,)\n",
    "display(arr2)\n",
    "arr3 = arr1 + arr2 # arr2를 복사하여 4번 브로드캐스트합니다. shape(4,3)\n",
    "arr3 # jupyter는 변수를 출력합니다. 【Pycharm은 해당 작업이 없습니다.】 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "07b4c133",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a2c2dd5",
   "metadata": {},
   "source": [
    "#### 2차원 배열 브로드캐스트"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "42e09cbf",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.sort(np.array([0,1,2,3]*3)).reshape(4,3) # shape(4,3)\n",
    "# arr2，이차원\n",
    "arr2 = np.array([[1],[2],[3],[4]]) # shape(4,1)\n",
    "display(arr1,arr2)\n",
    "arr3 = arr1 + arr2 # arr2 进行广播复制3份 shape(4,3)\n",
    "display(arr1,arr2,arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f54de90e",
   "metadata": {},
   "source": [
    "#### 고차원 배열 브로드캐스트"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c37cfadc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[0, 3]],\n",
       "\n",
       "       [[4, 0]],\n",
       "\n",
       "       [[4, 8]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(3, 1, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  4],\n",
       "        [ 2,  6],\n",
       "        [ 4,  8],\n",
       "        [ 6, 10]],\n",
       "\n",
       "       [[ 4,  1],\n",
       "        [ 6,  3],\n",
       "        [ 8,  5],\n",
       "        [10,  7]],\n",
       "\n",
       "       [[ 4,  9],\n",
       "        [ 6, 11],\n",
       "        [ 8, 13],\n",
       "        [10, 15]]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2) #shape(3,4,2)\n",
    "# arr2 = np.array([0,1,2,3,4,5,6,7]).reshape(4,2) #shape(4,2)\n",
    "# arr2 = np.random.randint(0,10,size = (3,4,1))\n",
    "arr2 = np.random.randint(0,10,size = (3,1,2))\n",
    "display(arr1.shape,arr2,arr2.shape)\n",
    "arr3 = arr1 + arr2 # arr2 배열의 0차원을 기준으로 3번 복사합니다. shape(3,4,2)\n",
    "arr3\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5309d6d0",
   "metadata": {},
   "source": [
    "### 일반 함수"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f66cc07a",
   "metadata": {},
   "source": [
    "#### 원소 수치 함수"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d946e07",
   "metadata": {},
   "source": [
    "abs、sqrt、square、exp、log、sin、cos、tan，maxinmum、minimum、all、any、inner、clip、round、trace、ceil、floor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b0f1a0da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.         2.         2.82842712 3.         4.         5.        ]\n",
      "[  1  16  64  81 256 625]\n",
      "[ 2  4  8  9 16 16]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([1,4,8,9,16,25])\n",
    "print(np.sqrt(arr1)) # 제곱근\n",
    "print(np.square(arr1)) # 제곱\n",
    "print(np.clip(arr1,2,16)) # array([ 2,  4,  8,  9, 16, 16])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8e039b89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.71828183,  7.3890561 , 20.08553692])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 자연상수를 밑으로 하는 지수 함수\n",
    "np.exp(np.array([1,2,3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "34e94dde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20.085536923187664"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.e**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5e64aa3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 5 3 9 3 9 8]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1,5,2,9,3,6,8])\n",
    "y = np.array([2,4,3,7,1,9,0])\n",
    "print(np.maximum(x,y)) # 두 배열 중 큰 값 반환"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "20da0629",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 99, 109, 138,  68,  55])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.random.randint(0,10,size = (5,5))\n",
    "np.inner(arr2[0],arr2) #1차원 배열 벡터 내적 반환"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "7f562b81",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 6, 2, 7, 3],\n",
       "       [9, 5, 2, 9, 1],\n",
       "       [8, 8, 5, 9, 3],\n",
       "       [8, 2, 5, 2, 8],\n",
       "       [3, 0, 1, 5, 5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 6, 2, 7, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr2,arr2[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "2e15981c",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99\n",
      "109\n",
      "138\n",
      "68\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print((arr2[0] * arr2[i]).sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbfaa876",
   "metadata": {},
   "source": [
    "#### where 함수 사용\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "865e62a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  5,  7, 10])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([1,3,5,7,9])\n",
    "arr2 = np.array([2,4,6,8,10])\n",
    "cond = np.array([True,False,True,True,False])\n",
    "np.where(cond,arr1,arr2) # True - arr1，False - arr2값\n",
    "#  array([ 1,  4,  5,  7, 10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "3af8cfa9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  0,  6, 15, 26,  1, 28, 23, 19, 21, 25, 20, 27,  0, 27, 19, 15,\n",
       "       12,  0, 24])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([  9,   0,   6, -15, -15,   1, -15, -15, -15, -15, -15, -15, -15,\n",
       "         0, -15, -15, -15,  12,   0, -15])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.random.randint(0,30,size = 20)\n",
    "display(arr3)\n",
    "np.where(arr3 < 15,arr3,-15) # 15보다 작으면 자기 자신의 값으로 유지하고, 15보다 크면 -15로 설정합니다 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "943c2fc1",
   "metadata": {},
   "source": [
    "#### 정렬 방법"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "eae624f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 11,  6, 17,  5,  4, 15,  1])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  4,  5,  6,  9, 11, 15, 17])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([9,3,11,6,17,5,4,15,1])\n",
    "display(arr)\n",
    "arr.sort() # 원래의 배열을 직접 수정합니다.\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d5dff01",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "a69caeb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  4,  5,  6,  9, 11, 15, 17])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([9,3,11,6,17,5,4,15,1])\n",
    "arr2 = np.sort(arr) # 깊은 복사본의 정렬 결과를 반환합니다.\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5f68bc3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 11,  6, 17,  5,  4, 15,  1])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr # 원래 데이터는 변경되지 않습니다"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "8e57d80b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 11,  6, 17,  5,  4, 15,  1])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([8, 1, 6, 5, 3, 0, 2, 7, 4], dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  4,  5,  6,  9, 11, 15, 17])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([9,3,11,6,17,5,4,15,1])\n",
    "index = arr.argsort() # 작은 값부터 큰 값 순으로 정렬된 인덱스를 반환합니다. (정렬 인덱스를 기반으로 나열합니다.)\n",
    "# array([8, 1, 6, 5, 3, 0, 2, 7, 4])\n",
    "display(arr,index)\n",
    "# 인덱스를 기반으로 정렬합니다.\n",
    "arr[index]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97659f1b",
   "metadata": {},
   "source": [
    "#### 집합 연산"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "f7bd75b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 6]\n",
      "[2 3 4 5 6 8]\n",
      "[2 8]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([2,4,6,8])\n",
    "B = np.array([3,4,5,6])\n",
    "print(np.intersect1d(A,B)) # 교집합 array([4, 6])\n",
    "print(np.union1d(A,B)) # 합집합 array([2, 3, 4, 5, 6, 8])\n",
    "print(np.setdiff1d(A,B)) #차집합，A에 있습니다，B에 없습니다 array([2, 8])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0f2eb5a",
   "metadata": {},
   "source": [
    "#### 수학 및 통계 함수"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ef2e663",
   "metadata": {},
   "source": [
    "min、max、mean、median、sum、std、var、cumsum、cumprod、argmin、argmax、argwhere、cov、corrcoef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "17223b24",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([1,7,2,19,23,0,88,11,6,11])\n",
    "print(arr1.min()) # 최솟값 계산 0\n",
    "index = arr1.argmin()\n",
    "arr1[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "85135a8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.argmax() # 최댓값의 인덱스 계산 (6을 반환)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "7de408ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[23],\n",
       "       [88]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.argwhere(arr1 > 20) # 20보다 큰 원소의 인덱스를 반환합니다\n",
    "arr1[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "89a6718d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   8,  10,  29,  52,  52, 140, 151, 157, 168])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cumsum(arr1) # 누적 합 계산"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "d7653998",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 4, 4, 1, 9],\n",
       "       [1, 7, 2, 5, 1],\n",
       "       [3, 2, 0, 3, 8],\n",
       "       [4, 6, 1, 5, 2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "3.7"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.random.randint(0,10,size = (4,5))\n",
    "display(arr2)\n",
    "arr2.mean() # 열의 평균값 계산"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "62836626",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.5 , 4.75, 1.75, 3.5 , 5.  ])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2_mean = arr2.mean(axis = 0) # 이것이 행인지 열인지 계산합니다.\n",
    "display(arr2_mean,arr2_mean.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "6fa14903",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.8, 3.2, 3.2, 3.6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2_mean = arr2.mean(axis = 1) # 이것이 행인지 열인지 계산합니다.\n",
    "display(arr2_mean,arr2_mean.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "f04e97d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.210000000000001"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(arr2) # 분산 계산"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "09f3fb61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.210000000000001"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((arr2 - arr2.mean())**2).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "e7c44be5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 4, 4, 1, 9],\n",
       "       [1, 7, 2, 5, 1],\n",
       "       [3, 2, 0, 3, 8],\n",
       "       [4, 6, 1, 5, 2]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "2c92dea3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 8.7 , -5.2 ,  6.05, -2.85],\n",
       "       [-5.2 ,  7.2 , -2.8 ,  4.35],\n",
       "       [ 6.05, -2.8 ,  8.7 , -0.65],\n",
       "       [-2.85,  4.35, -0.65,  4.3 ]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2,rowvar=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "28db1a21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(6.96)"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2[0],ddof=0) # 공분산 계산"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "e948fcb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.959999999999999"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(((arr2[0] - arr2[0].mean())**2),2).sum()/5 # 方差\n",
    "# 분산에는 두 가지 계산 공식이 있습니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "3d5c8a84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.7"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((arr2[0] - arr2[0].mean())**2).sum()/(5-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "3cbfdfdf",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4.33333333, -1.83333333,  1.83333333, -3.        ,  5.66666667],\n",
       "       [-1.83333333,  4.91666667,  0.91666667,  2.83333333, -8.        ],\n",
       "       [ 1.83333333,  0.91666667,  2.91666667, -1.83333333,  1.66666667],\n",
       "       [-3.        ,  2.83333333, -1.83333333,  3.66666667, -7.33333333],\n",
       "       [ 5.66666667, -8.        ,  1.66666667, -7.33333333, 16.66666667]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2,rowvar=False) # 공분산 행렬 계산"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "38ed60de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        , -0.6570183 ,  0.6954023 , -0.46596254],\n",
       "       [-0.6570183 ,  1.        , -0.35377909,  0.78178761],\n",
       "       [ 0.6954023 , -0.35377909,  1.        , -0.10627216],\n",
       "       [-0.46596254,  0.78178761, -0.10627216,  1.        ]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 범위 -1부터 1까지\n",
    "np.corrcoef(arr2,rowvar=True) # 상관계수 계산"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a20d579",
   "metadata": {},
   "source": [
    "### 선형대수"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "faa86500",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#행렬 곱셈\n",
    "A = np.array([[4,2,3],\n",
    "              [1,3,1]]) # shape(2,3)\n",
    "B = np.array([[2,7],\n",
    "              [-5,-7],\n",
    "              [9,3]]) # shape(3,2)\n",
    "np.dot(A,B) # 행렬 연산에서 A의 마지막 차원과 B의 첫 번째 차원이 일치해야합니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "d6f3fcc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B # 기호 @는 행렬 곱셈 연산을 나타냅니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "be6069b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 7)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.random.randint(0,10,size = (3,6))\n",
    "B = np.random.randint(0,10,size = (6,7))\n",
    "A.dot(B).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "9cf5611f",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(suppress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "9bce0f91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2. ,  0.5,  0.5],\n",
       "       [ 0. ,  2. , -1. ],\n",
       "       [ 1. , -1.5,  0.5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "-2.0000000000000004"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#행렬의 역 계산\n",
    "from numpy.linalg import inv,det,eig,qr,svd\n",
    "A = np.array([[1,2,3],\n",
    "              [2,3,4],\n",
    "              [4,5,8]]) # shape(3,3)\n",
    "B = inv(A) # 역 행렬\n",
    "display(B)\n",
    "display(A @ B)\n",
    "det(A)#행렬의 행렬식 계산"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b89546ba",
   "metadata": {},
   "source": [
    "### homework"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "c2eed6ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3, 4, 5)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.random.randint(0,10,size = (2,3,4,5))\n",
    "display(arr.shape)\n",
    "# \n",
    "arr.sum(axis = (0,-1)).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e253980",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
